//
//  VenuelistViewC.m
//  WHSport
//
//  Created by ccjoy-jimneylee on 15/3/25.
//  Copyright (c) 2015年 jimneylee. All rights reserved.
//

#import "VenuelistViewC.h"
#import "MainViewController.h"
#import "BusinessModel.h"
#import "PlaceEntity.h"
#import <UIImageView+AFNetworking.h>
#import "PlaceDetailViewC.h"
#import "Util.h"

@interface ListCell : UITableViewCell
@property(nonatomic, weak) IBOutlet UIImageView *placeImg;
@property(nonatomic, weak) IBOutlet UIImageView *levelImg;
@property(nonatomic, weak) IBOutlet UILabel *nameLab;
@property(nonatomic, weak) IBOutlet UILabel *evaLab;
@property(nonatomic, weak) IBOutlet UILabel *nowLab;
@property(nonatomic, weak) IBOutlet UILabel *oriLab;
@property(nonatomic, weak) IBOutlet UILabel *placeLab;
@property(nonatomic, weak) IBOutlet UILabel *disLab;

@end
@implementation  ListCell

@end

@interface VenuelistViewC ()
{
    NSString *num;
    NSArray *allArray;
    NSArray *placeArray;
    NSArray *screenArray;
    NSString *iarea;
    NSString *isort;
    NSArray *infoArray;
}
@end

@implementation VenuelistViewC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self getData];
    self.title =@"挥汗网";
    _typeTableView.hidden = YES;
    
    if ([_allBtn isSelected]) {
        num =@"1";
    }
    
    allArray = @[@"全部分类",@"羽毛球",@"乒乓球",@"足球",@"篮球",@"网球",
                 @"台球",@"钓鱼",@"攀岩"];
    placeArray = @[@"全部商区",@"钟楼区",@"天宁区",@"武进区",@"新北区",
                   @"戚墅堰区",@"金坛市",@"溧阳市"];
    screenArray = @[@"原来排序",@"评价最高",@"价格最低"];
    
    
    if ([CLLocationManager locationServicesEnabled]) {
        self.locationManager = [[CLLocationManager alloc] init];
        
        _locationManager.delegate = self;
        _locationManager.desiredAccuracy = kCLLocationAccuracyBest; //控制定位精度,越高耗电量越大。
        _locationManager.distanceFilter = 2000.0f;
        
        SEL method = NSSelectorFromString(@"requestAlwaysAuthorization");
        if ([_locationManager respondsToSelector:method]) {
            [_locationManager requestAlwaysAuthorization];
            if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
                [_locationManager requestWhenInUseAuthorization];
                //使用程序其间允许访问位置数据
            }
        }
    }

    
    [_typeTableView reloadData];
    [_listTableView reloadData];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [_locationManager startUpdatingLocation];
}

-(void) viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [_locationManager stopUpdatingLocation];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField;
{
    [textField resignFirstResponder];
    return YES;
}

#pragma mark - CLLocationManagerDelegate

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
    
    CLLocation * currLocation = [locations lastObject];
    self.latitude = [NSString stringWithFormat:@"%f",currLocation.coordinate.latitude];
    self.longitude = [NSString stringWithFormat:@"%f",currLocation.coordinate.longitude];
//    NSLog(@"%@,%@",self.latitude,self.longitude);
}

-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
    if ([error code]==kCLErrorDenied) {
        NSLog(@"访问被拒绝");
    }
    if ([error code]==kCLErrorLocationUnknown) {
        NSLog(@"无法获取位置信息");
    }
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (IBAction)allBtn:(id)sender{
    if ( _typeTableView.hidden == NO) {
         _typeTableView.hidden = YES;
    }
    else{
    _typeTableView.hidden = NO;
    num =@"1";
    [_typeTableView reloadData];
    }
}
- (IBAction)placeBtn:(id)sender{
    if ( _typeTableView.hidden == NO) {
        _typeTableView.hidden = YES;
    }
    else{
    _typeTableView.hidden = NO;
    num =@"2";
    [_typeTableView reloadData];
    }

}
- (IBAction)screenBtn:(id)sender{
    if ( _typeTableView.hidden == NO) {
        _typeTableView.hidden = YES;
    }
    else{
    _typeTableView.hidden = NO;
    num =@"3";
    [_typeTableView reloadData];
    }
}
- (IBAction)searchBtn:(id)sender{
    [self getData];
}


- (void)getData{
    
    if (self.type != nil) {
        self.all.text = self.type;
    }
    NSString *vlatitude =@"32.111111";
    NSString *vlongitude =@"119.111111";
    NSString *vtype;
    
    if (self.itype.length == 0) {
        vtype =@"1";
    }
    else{
        vtype =self.itype;
    }
    NSString *varea =@"1";
    NSString *vsort;
    NSString *vpage =@"1";
    NSString *vname = _placeField.text;
    if (self.latitude.length != 0) {
        vlatitude =self.latitude;
    }
    if (self.longitude.length != 0) {
        vlongitude =self.longitude;
    }
    if (vname.length == 0) {
        vname =@"";
    }
    
    if ([self.all.text isEqualToString:@"全部分类"]) {
        vtype =@"2";
    }
    else{
        
        vtype = self.itype;
    }
//    if ([self.place.text isEqualToString:@"全部商区"]) {
//        varea =@"2";
//    }
//    else{
//        varea = iarea;
//    }
    if ([self.screen.text isEqualToString:@"智能筛选"]) {
        vsort =@"2";
    }
    else{
        vsort = isort;
    }
    
    
    [[BusinessModel sharedInstance] getDataWithLatitude:vlatitude longitude:vlongitude page:vpage sportsItemId:vtype areaId:varea sort:vsort sprotsHallName:vname getPlaceList:^(id info) {
        NSArray *ainfo = [info copy];
        infoArray = ainfo;
        [_listTableView reloadData];
        [_typeTableView reloadData];
    } ];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (tableView == _listTableView) {
        return 88.f;
    }
    
    return 44.f;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView == _typeTableView) {
        if ([num isEqualToString:@"1"]) {
            return 9;
        }
        if ([num isEqualToString:@"2"]) {
            return 8;
        }
        if ([num isEqualToString:@"3"]) {
            return 3;
        }
    }
        return infoArray.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString *identifier;
    
    if (tableView == _typeTableView) {
        UITableViewCell *cell;
        identifier =@"TypeCell";
        cell = [tableView dequeueReusableCellWithIdentifier:identifier
                                               forIndexPath:indexPath];
        
        if ([num isEqualToString:@"1"]) {
            cell.textLabel.text = allArray[indexPath.row];
        }
        
        if ([num isEqualToString:@"2"]) {
            cell.textLabel.text = placeArray[indexPath.row];
        }
        if ([num isEqualToString:@"3"]) {
            cell.textLabel.text = screenArray[indexPath.row];
        }
        
        return cell;
    }
    else{
        
        identifier =@"ListCell";
        
        ListCell *cell =[tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
        PlaceEntity *placeEntity = infoArray[indexPath.row];
        
//        NSString *name = [NSString stringWithFormat:@"%@",ety.name];
        cell.nameLab.text = placeEntity.name;

        NSString *eva = [NSString stringWithFormat:@"%@评论",
                         placeEntity.evaluationCount];
        cell.evaLab.text = eva;
        NSString *now = [NSString stringWithFormat:@"%@",
                         placeEntity.nowPrice];
        cell.nowLab.text = now;
        NSString *ori = [NSString stringWithFormat:@"%@元",
                         placeEntity.originalPrice];
        cell.oriLab.text = ori;
        cell.placeLab.text = placeEntity.address;
        NSString *dis = [NSString stringWithFormat:@"%@km",
                         placeEntity.distance];
        cell.disLab.text = dis;
        NSString *level = [NSString stringWithFormat:@"%@",
                           placeEntity.level];
        NSString *levelImg = [Util getstrWithstarNum:level];
        cell.levelImg.image = [UIImage imageNamed:levelImg];
        NSString *img = [NSString stringWithFormat:@"http://218.93.12.178:8080/hhw-api/%@",
                         placeEntity.imgPath];
        NSURL *url = [NSURL URLWithString:img];
        [cell.placeImg setImageWithURL:url];
        
        return cell;

    }
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.selectionStyle =UITableViewCellSelectionStyleNone;

    if (tableView == _typeTableView) {
        if ([num isEqualToString:@"1"]) {
            self.all.text = allArray[indexPath.row];
            self.itype = [NSString stringWithFormat:@"%ld",
                          indexPath.row];
            [self getData];
            _typeTableView.hidden = YES;
        }
        
        if ([num isEqualToString:@"2"]) {
            self.place.text = placeArray[indexPath.row];
            iarea = [NSString stringWithFormat:@"%ld",indexPath.row];
            [self getData];
            _typeTableView.hidden = YES;
        }
        if ([num isEqualToString:@"3"]) {
            self.screen.text = screenArray[indexPath.row];
            isort = [NSString stringWithFormat:@"%ld",indexPath.row+1];
            [self getData];
            _typeTableView.hidden = YES;
        }

    }
    if (tableView == _listTableView){
        
        if ([self.where isEqualToString:@"1"]) {
            PlaceEntity *placeEntity = infoArray[indexPath.row];
            NSString *name = placeEntity.name;
            NSString *idNum = [NSString stringWithFormat:@"%@",
                               placeEntity.ID];
            NSDictionary *dic =@{@"id":idNum,@"name":name};
            [[NSNotificationCenter defaultCenter] postNotificationName:@"NSNotification_PLACE" object:dic];
            self.where = nil;
            [self.navigationController popViewControllerAnimated:YES];
        }
        
        else{
            UIStoryboard *main = [UIStoryboard storyboardWithName:@"Main"
                                                           bundle:nil];
            PlaceDetailViewC *placevc = [main instantiateViewControllerWithIdentifier:@"PlaceDetailViewC"];
            placevc.currentPlace = infoArray[indexPath.row];
            [self.navigationController pushViewController:placevc
                                                 animated:YES];
        }
    
    }
    
}


@end
